package com.yc.bdw.Activity;


import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yc.bdw.Adapter.ShopCarListAdapter;
import com.yc.bdw.R;
import com.yc.bdw.model.ShopCarItem;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shuang on 2015/11/5.
 */
public class ShoppingCarActivity extends KJActivity {

    @BindView(id = R.id.shopcar_edit, click = true)
    private Button editText;
    @BindView(id = R.id.shopcar_selectAll, click = true)
    private CheckBox selectAll;
    @BindView(id = R.id.shopcar_compute, click = true)
    private View computeLayout;
    @BindView(id = R.id.shopcar_compute_text) //右下角按钮的文字内容
    private View computText;
    @BindView(id = R.id.shopcar_price)  //总价钱
    private TextView priceText;
    @BindView(id = R.id.shopcar_price_head)  //总价钱之前的文字
    private TextView priceHeadText;
    @BindView(id = R.id.shopcar_delete)  //总价钱之前的文字
    private TextView deleteText;
    @BindView(id = R.id.shopcar_products_list)
    private ListView productsList;

    @BindView(id=R.id.btn_home,click = true)
    private ImageView btn_home;
    @BindView(id=R.id.btn_sort,click = true)
    private ImageView  btn_sort;
    @BindView(id=R.id.btn_shoppingcart,click = true)
    private ImageView  btn_shoppingcart;
    @BindView(id=R.id.btn_myself,click = true)
    private ImageView  btn_myself;

    private ShopCarListAdapter adapter;
    private List<ShopCarItem> shopCarList = new ArrayList<ShopCarItem>();

    private static boolean isEdit = false;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    public void setRootView() {
        super.setContentView(R.layout.activity_shoppingcar);
    }

    @Override
    public void initData() {
        super.initData();

        init();

        adapter = new ShopCarListAdapter(aty);
        adapter.setList(shopCarList);
        adapter.init();
    }

    private void init() {
        ShopCarItem item = new ShopCarItem();
        item.setPrice(BigDecimal.valueOf(11));
        item.setDescribe("220ml");
        item.setProductName("酒1");
        item.setNumber(2);
        shopCarList.add(item);

        ShopCarItem item2 = new ShopCarItem();
        item2.setPrice(BigDecimal.valueOf(22));
        item2.setDescribe("220ml");
        item2.setProductName("酒2");
        item2.setNumber(2);
        shopCarList.add(item2);

        ShopCarItem item3 = new ShopCarItem();
        item3.setPrice(BigDecimal.valueOf(33));
        item3.setDescribe("220ml");
        item3.setProductName("酒3");
        item3.setNumber(2);
        shopCarList.add(item3);

        ShopCarItem item4 = new ShopCarItem();
        item4.setPrice(BigDecimal.valueOf(44));
        item4.setDescribe("220ml");
        item4.setProductName("酒4");
        item4.setNumber(2);
        shopCarList.add(item4);
    }

    @Override
    public void initWidget() {
        super.initWidget();

        btn_home.setBackgroundResource(R.drawable.btn_home_normal);
        btn_shoppingcart.setBackgroundResource(R.drawable.btn_shopping_pre);

        selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean flag) {
                shopCarList = adapter.getList();
                if (flag) {
                    for (int i = 0; i < shopCarList.size(); i++) {
                        ShopCarListAdapter.getIsSelected().put(i, true);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i < shopCarList.size(); i++) {
                        ShopCarListAdapter.getIsSelected().put(i, false);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });

        productsList = (ListView) this.findViewById(R.id.shopcar_products_list);
        productsList.setAdapter(adapter);

        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if ( selectAll.isChecked() ) {
                    selectAll.setChecked(false);
                } else {
                    for (int i = 0; i < shopCarList.size(); i++) {
                        ShopCarListAdapter.getIsSelected().put(i, false);
                    }
                    adapter.notifyDataSetChanged();
                }
                return false;
            }
        });
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.shopcar_edit:
                if ( !isEdit ) {
                    editText.setText("完成");
                    priceHeadText.setVisibility(View.GONE);
                    priceText.setVisibility(View.GONE);
                    computText.setVisibility(View.GONE);
                    deleteText.setVisibility(View.VISIBLE);

                    isEdit = true;
                } else {
                    editText.setText("编辑");
                    priceHeadText.setVisibility(View.VISIBLE);
                    priceText.setVisibility(View.VISIBLE);
                    computText.setVisibility(View.VISIBLE);
                    deleteText.setVisibility(View.GONE);

                    isEdit = false;
                }
                break;
            case R.id.shopcar_compute:
                if ( isEdit ) {
                    shopCarList = adapter.getList();
                    for ( int i = 0; i < shopCarList.size(); i++) {
                        if ( ShopCarListAdapter.getIsSelected().get(i) ) {
                            ShopCarListAdapter.getIsSelected().remove(i);
                            shopCarList.get(i).setNumber(-1);
                        }
                    }

                    for ( int i = 0; i < shopCarList.size(); i++ ) {
                        if ( shopCarList.get(i).getNumber() == -1 ) {
                            shopCarList.remove(i);
                            i--;
                        }
                    }

                    for ( int i = 0; i < ShopCarListAdapter.getIsSelected().size(); i++ ) {
                        ShopCarListAdapter.getIsSelected().put(i,false);
                    }
                    adapter.setList(shopCarList);
                    adapter.notifyDataSetChanged();
                } else {
                    showActivity(aty,OrderConfirmActivity.class);
                }
                break;
            case R.id.btn_home:
                showActivity(aty, HomeActivity.class);
                break;
            case R.id.btn_sort:
                showActivity(aty,SortListActivity.class);
                break;
            case R.id.btn_myself:
                break;
        }
    }

    public static boolean isEdit() {
        return isEdit;
    }

    public static void setIsEdit(boolean isEdit) {
        ShoppingCarActivity.isEdit = isEdit;
    }
}
